import {defineStore} from "pinia";
import {ref, computed} from 'vue'

import analyzeData from "../apis/anlyze.js";

export  const useMapAddress = defineStore("mapAdress", ()=>{
    //数据
    //老人的基本信息
    const elderlyList =ref([

       // "长沙喜盈门范城亚朵酒店",
       //  "莫林酒店长沙红星民政学院店",
       //  "长沙延年文苑酒店",
       //  "廷泊酒店",
       //  "维也纳国际酒店（长沙红星德思勤广场店",
       //  "绿草地（喜盈门店）",
       //  "南景饭店（圭塘河店）",
       //  "喜庭海鲜（星城天地店）"
    ])
    //志愿者的基本信息
    const volunteerList =ref([])
    //任务的基本信息
    const taskList =ref([])

    //中心点
    //elderlyMap center
    const elderlyCenter = ref({
        lng:112.9388,lat:28.2282
    })
    //volunteer Map center
    const volunteerCenter = ref({
        lng:112.9388,lat:28.2282
    })
    // task Map center
    const  taskCenter =ref({
        lng:112.9388,lat:28.2282
    })

    //getter方法
    //获取state
    const getList= (category)=>{
        if(category.startsWith("V")){
            return volunteerList.value
        }else if(category.startsWith("E")){
            return elderlyList.value
        }else if(category.startsWith("T")){
            return taskList.value
        }else{
            alert("类别为空")
        }

    }
    //获取中心点
    const getCenter =(category)=>{
        if(category.startsWith("V")){
            return volunteerCenter.value
        }else if(category.startsWith("E")){
            return elderlyCenter.value
        }else if(category.startsWith("T")){
            return taskCenter.value
        }else{
            alert("中心点不准")
        }

    }
    const setCenterStore =(point,category)=>{
        if(category.startsWith("V")){
            volunteerCenter.value ={lng:point.lng,lat:point.lat}
        }else if(category.startsWith("E")){
            elderlyCenter.value ={lng:point.lng,lat:point.lat}
        }else if(category.startsWith("T")){
            taskCenter.value ={lng:point.lng,lat:point.lat}
        }else{
            alert("类别缺少")
        }

    }
    //调用接口,获取数据
    const fetchDataElderly = async () => {
            try {
                elderlyList.value = await analyzeData.getElderlyList();

            } catch (error) {
                console.log(error);
            }
        };
    return {elderlyList,volunteerList,taskList,getList,setCenterStore,getCenter,fetchDataElderly};
        //

},
    {
        persist: {
            enabled:true,
            strategies:[
                {
                    key:'address',
                    storage:localStorage,
                    paths:['elderlyList','taskCenter','volunteerList','taskList','volunteerCenter','elderlyCenter']
                }
            ]
        }
    })